from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.
from itsdangerous import TimedJSONWebSignatureSerializer


class User(AbstractUser):
    """用户模型类"""
    # mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')
    mobile = models.CharField(max_length=11, unique=True, verbose_name="手机号")
    email_active = models.BooleanField(default=False, verbose_name='邮箱验证状态')

    def generate_verify_email_url(self):
        '''生成激活邮箱的链接'''

         # 使用itsdangerous对 user_id 和 email 进行加密
        s = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, 60*60*24)
        token = s.dumps({'user_id': self.id, 'email': self.email}).decode()

        # 调用return 对邮箱激活链接进行拼接
        return 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token

    class Meta:
        db_table = 'tb_users'  # 表名
        verbose_name = '用户'  # 管理后台的名字
        verbose_name_plural = verbose_name